package LeetCode;

public class LC_670_MaximumSwap {

    public static void main(String[] args) {

    }

    class Solution {
        public int maximumSwap(int num) {
            char[] digit = Integer.toString(num).toCharArray();
            int[] loc = new int[10];

            for (int i = 0; i < digit.length; i++) {
                loc[digit[i] - '0'] = i;
            }
            for (int i = 0; i < digit.length; i++) {
                for (int j = 9; j > digit[i] - '0'; j--) {
                    if (i < loc[j]) {
                        char tmp = digit[i];
                        digit[i] = digit[loc[j]];
                        digit[loc[j]] = tmp;
                        return Integer.valueOf(new String(digit));
                    }
                }
            }
            return num;
        }
    }

}